﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WhittleList.Helpers;
using WhittleList.Bll;

namespace WhittleList.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {
        public HomeController() : this(new UserRepository(), new SiteRepository())
        {
        }

        public HomeController(UserRepository userRepository, ISiteRepository siteRepository) : base(userRepository, siteRepository)
        {
        }

        public ActionResult Index()
        {
            //not loged in main site
            if(CurrentUser == null && CurrentSite == null)
            {
                return View("Welcome");
            }
            //not logged in but on site
            else if (CurrentUser == null && CurrentSite != null)
            {
                throw new NotImplementedException();
                return Redirect(Url.Site(CurrentSite, "Welcome"));
            }
            //logged in and on a site with rights
            else if (CurrentSite != null && SiteRepository.UserHasRole(CurrentUser, CurrentSite))
            {
                throw new NotImplementedException();
                return Redirect(Url.Site(CurrentSite, "Dashboard"));
            }
            //logged in and on a site with rights
            else if (CurrentSite != null && CurrentUser.Id != CurrentSite.OwnerId)
            {
                throw new NotImplementedException();
                return View("NoRole");
            }
            //Logged in but site does not exist
            else if (!IsMainSite())
            {
                throw new NotImplementedException();
                return RedirectToAction("SetUp", "Site");
            }
            //logged in on the main site
            else
            {
                return RedirectToAction("Main", "Dashboard");
            }
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Html()
        {
            return View();
        
        }
    }
}
